import {
	formatTime
} from "@/utils/formatTime";
import {
	i18n
} from '@/main.js'
import vueMain from "@/main.js";

// 星期格式转换 0-6：周日到周六
const weekFormat = (e) => {
	switch (e) {
		case 0:
			return i18n.t("time.Sunday");
		case 1:
			return i18n.t("time.Monday");
		case 2:
			return i18n.t("time.Tuesday");
		case 3:
			return i18n.t("time.Wednesday");
		case 4:
			return i18n.t("time.Thursday");
		case 5:
			return i18n.t("time.Friday");
		case 6:
			return i18n.t("time.Saturday");
		default:
			return "";
	}
};
// 转换今日的时辰格式
const todayTimeFormat = (e) => {
	if (e >= 0 && e < 7) {
		return i18n.t("time.MorningBefore");
	} else if (e >= 7 && e < 11) {
		return i18n.t("time.Morning");
	} else if (e >= 11 && e < 13) {
		return i18n.t("time.Midday");
	} else if (e >= 13 && e < 18) {
		return i18n.t("time.Afternoon");
	} else if (e >= 18 && e < 24) {
		return i18n.t("time.Night");
	} else {
		return "";
	}
};

// 是否显示周几
const isShowWeekDay = (sub, weekDay) => {
	const currentWeekDay = new Date().getDay();
	const dayTime = 1000 * 60 * 60 * 24;
	// 1.当前时间与消息时间相差必须大于2天小于7天
	// 2.当前时间距离本周一相差必须大于2天且小于当前距离周一的天数
	// 3.消息时间不可能是0-周日，因为周日没有给后面时间留空间，不会走这里的逻辑，而是走今天的逻辑
	if (sub >= dayTime * 2 && weekDay !== 0 && sub <= dayTime * currentWeekDay) {
		return true;
	} else {
		return false;
	}
};

// 仿微信时间显示格式转换 @time 时间戳毫秒
const weChatTimeFormat = (time) => {
	const today = new Date().getTime();
	// 当前时间减去获取到的时间
	const sub = today - time;
	const day = 1000 * 60 * 60 * 24;
	const timeDate = new Date(time);
	const currentYear = new Date().getFullYear();
	const getYear = new Date(time).getFullYear();
	const HHmm = `${formatTime("hh", timeDate)}:${formatTime("mm", timeDate)}`;
	const showWeekDay = isShowWeekDay(sub, timeDate.getDay());
	if (currentYear > getYear) {
		return `${formatTime("yyyy/MM/dd", timeDate)} ${todayTimeFormat(
      timeDate.getHours()
    )} ${formatTime("hh:mm", timeDate)}`;
	} else if (showWeekDay) {
		return `${weekFormat(timeDate.getDay())} ${HHmm}`;
	} else if (sub > day && sub < day * 2) {
		return i18n.t("time.Yesterday");
	} else if (sub <= day) {
		return HHmm;
	} else {
		return `${formatTime("MM/dd", timeDate)} ${formatTime("hh:mm", timeDate)}`;
	}
};


const getRandomColor = () => {
	const colorList = [
		'linear-gradient(180deg, #7CDBB1 0%, #61BD95 100%)',
		'linear-gradient(180deg, #8BD7F6 0%, #5CBDE5 99%)',
		'linear-gradient(180deg, #F28E26 0%, #FD644F 100%)',
		'linear-gradient(180deg, #F7CB6B 0%, #FBA980 100%)',
		'linear-gradient(180deg, #7C8AE6 0%, #A8B2F5 100%)',
	]

	const romdomIndex = parseInt(Math.random() * (colorList.length))
	return colorList[romdomIndex]
}
const orderStatusText = (orderStatus) => {
	// 0 已冻结 1正在出售 2等待付款 10已付款 20已完成  30投诉 40已取消
	let statusText
	switch (orderStatus) {
		case 2:
			statusText = i18n.t('order.Pending-payment');
			break;
		case 30:
			statusText = i18n.t('order.Complaint');
			break;
		case 10:
			statusText = i18n.t('order.Already-paid');
			break;
		case 40:
			statusText = i18n.t('order.Cancelled');
			break;
		case 20:
			statusText = i18n.t('order.Complete');
		default:
			break;
	}
	return statusText;
}

// 聊天列表时间格式转化
const ChatListTimeFormat = (time) => {
	if (!time) {
		return ''
	}
	const today = new Date().getTime()
	// 当前时间减去获取到的时间
	const sub = today - time
	const day = 1000 * 60 * 60 * 24
	const timeDate = new Date(time)
	const currentYear = new Date().getFullYear()
	const getYear = new Date(time).getFullYear()
	const currentDay = new Date().getDate()
	const getDay = new Date(time).getDate()
	const HHmm = `${formatTime('hh', timeDate)}:${formatTime('mm', timeDate)}`
	// const showWeekDay = isShowWeekDay(sub, timeDate.getDay())
	if (currentYear > getYear) {
		return `${formatTime('yyyy-MM-dd', timeDate)}`
	} else if (sub > day && sub < day * 2) {
		if (currentDay == getDay - 1) {
			return vueMain.$t('index.Yesterday')
		} else {
			return `${formatTime('MM/dd', timeDate)}`
		}
	} else if (sub <= day) {
		if (currentDay == getDay) {
			return HHmm
		} else {
			return vueMain.$t('index.Yesterday')
		}
	} else {
		return `${formatTime('MM/dd', timeDate)}`
	}
}

/**
 * 重写console.log 方法，在生产环境时不打印
 */
const rewirteLog = () => {
  console.log = (function (log) {
    return process.env.NODE_ENV == 'production' ? function () { } : log
  }(console.log))
}

export {
	weekFormat,
	todayTimeFormat,
	isShowWeekDay,
	weChatTimeFormat,
	getRandomColor,
	orderStatusText,
	ChatListTimeFormat,
	rewirteLog
};